package com.zadtek.shopmall.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.zadtek.shopmall.App;
import com.zadtek.shopmall.Event.EventTest;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.activities.BrowingHistoryActivity;
import com.zadtek.shopmall.activities.CollectProductActivity;
import com.zadtek.shopmall.activities.EvaluateActivity;
import com.zadtek.shopmall.activities.LikeShopActivity;
import com.zadtek.shopmall.activities.LoginActivity;
import com.zadtek.shopmall.activities.MangeMoneyActivity;
import com.zadtek.shopmall.activities.OrderDetailActivity;
import com.zadtek.shopmall.activities.RedEnvelopActivity;
import com.zadtek.shopmall.activities.ReturnOrChangeActivity;
import com.zadtek.shopmall.activities.SettingActivity;
import com.zadtek.shopmall.activities.WebActivity;
import com.zadtek.shopmall.base.AbsFragment;
import com.zadtek.shopmall.bean.MeBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import q.rorbin.badgeview.QBadgeView;

import static com.zadtek.shopmall.activities.RedEnvelopActivity.TYPE;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TYPE_COUPON;
import static com.zadtek.shopmall.activities.RedEnvelopActivity.TYPE_RED;

/**
 * Created by Zackv on 2018/4/2.
 */

public class MeFragment extends AbsFragment implements View.OnClickListener {
    private TextView tvUserName,tvUserLevel,tvMyOrder,tvAllMyorder;
    private TextView tvUnpay,tvUnreceipt,tvUnevluated,tvReturnOrChange;
    private TextView tvMyMoney,tvMangeMoney,tvLeftMoney,tvRedEnvelop,tvIntegral,tvCoupon;
    private TextView tvCollectProducts,tvLikeShops,tvMyShare,tvBrowingHistory;
    private ImageView ivSetting,ivMessage,ivUserImg;
    private LinearLayout llLeftMoney,llRedEnvelop,llIntegral,llCoupon,llContent,llNologin;
    private TextView tvLogin;
    private MeBean meBean;
    @Override
    public int setView() {
        return R.layout.fragment_me;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        initView();
        initLogic();
        resetUI();
        if(!Const.isLogin){
            startActivity(new Intent(mActivity,LoginActivity.class));
        }else{
            requestData();
        }
    }

    private void resetUI() {
        ivUserImg.setImageDrawable(getResources().getDrawable(R.drawable.avatar));
        tvUserName.setText("立即登录");
        tvUserLevel.setText("");
    }

    private void requestData() {
//        meBean = new MeBean();
//        meBean.setBalance(5600.00);
//        meBean.setRedEnvelopNum(1);
//        meBean.setIntergral(344);
//        meBean.setCoupon(3);
//        meBean.setUnPayNum(5);
//        meBean.setUnReceiveNum(1);
//        meBean.setUnEvaluateNum(2);
//        meBean.setChangeNum(1);
//        meBean.setUserAvatar("https://pic2.zhimg.com/80/v2-1e02c1531c33f9460ae82eb88a999cdd_hd.jpg");
//        meBean.setUserName("行一度空山");
//        meBean.setUserLevel("普通用户啊");
//
//        BaseResponse<MeBean> baseResponse = new BaseResponse();
//        baseResponse.setMessage("");
//        baseResponse.setCode(0);
//        baseResponse.setData(meBean);
//        Gson gson = new Gson();
//        String json = gson.toJson(baseResponse);
       // updateView();
//
        NetTool.getApi().getMeData(NetTool.newParams())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<MeBean>>(this) {
                    @Override
                    public void onData(BaseResponse<MeBean> value) {
                        meBean = value.getData();
                        updateView();
                    }
                });
    }

    private void updateView() {
        new QBadgeView(mActivity).bindTarget(tvUnpay).setBadgeNumber(meBean.getPay_count());
        new QBadgeView(mActivity).bindTarget(tvUnreceipt).setBadgeNumber(meBean.getConfirmed_count());
        new QBadgeView(mActivity).bindTarget(tvUnevluated).setBadgeNumber(meBean.getNot_comment());
        new QBadgeView(mActivity).bindTarget(tvReturnOrChange).setBadgeNumber(meBean.getReturn_count());

        Glide.with(mActivity).load(meBean.getUser_picture()).dontAnimate().listener(mRequestListener).placeholder(R.drawable.avatar).into(ivUserImg);
        tvUserName.setText(meBean.getNick_name());
        tvUserLevel.setText(meBean.getRank_name());

        tvLeftMoney.setText(String.valueOf(meBean.getUser_money()));
        tvRedEnvelop.setText(String.valueOf(meBean.getBonus()));
        tvIntegral.setText(String.valueOf(meBean.getPay_points()));
        tvCoupon.setText(String.valueOf(meBean.getCouponses()));
    }
    RequestListener mRequestListener = new RequestListener() {
        @Override
        public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }

    };
    private void initLogic() {
        tvMyOrder.setOnClickListener(this);
        tvMyShare.setOnClickListener(this);
        tvAllMyorder.setOnClickListener(this);
        tvUnpay.setOnClickListener(this);
        tvUnreceipt.setOnClickListener(this);
        tvUnevluated.setOnClickListener(this);
        tvReturnOrChange.setOnClickListener(this);
        tvMyMoney.setOnClickListener(this);
        tvMangeMoney.setOnClickListener(this);
        tvCollectProducts.setOnClickListener(this);
        tvLikeShops.setOnClickListener(this);
        tvBrowingHistory.setOnClickListener(this);
        llLeftMoney.setOnClickListener(this);
        llRedEnvelop.setOnClickListener(this);
        llIntegral.setOnClickListener(this);
        llCoupon.setOnClickListener(this);
        ivSetting.setOnClickListener(this);
        ivMessage.setOnClickListener(this);
        tvUserName.setOnClickListener(this);
    }

    private void initView() {
        tvUserName = (TextView) findViewById(R.id.tv_username);
        tvUserLevel = (TextView) findViewById(R.id.tv_userlevel);
        tvMyOrder = (TextView) findViewById(R.id.tv_myorder);
        tvAllMyorder = (TextView) findViewById(R.id.tv_all_myorder);
        tvUnpay = (TextView) findViewById(R.id.tv_unpay);
        tvUnreceipt = (TextView) findViewById(R.id.tv_unreceipt);
        tvUnevluated = (TextView) findViewById(R.id.tv_unevluated);
        tvReturnOrChange = (TextView) findViewById(R.id.tv_return_or_change);
        tvMyMoney = (TextView) findViewById(R.id.tv_mywallet);
        tvMangeMoney = (TextView) findViewById(R.id.tv_mange_money);
        tvLeftMoney = (TextView) findViewById(R.id.tv_left_money);
        tvRedEnvelop = (TextView) findViewById(R.id.tv_red_envelop);
        tvIntegral = (TextView) findViewById(R.id.tv_integral);
        tvCoupon = (TextView) findViewById(R.id.tv_coupon);
        tvCollectProducts = (TextView) findViewById(R.id.tv_collect_products);
        tvLikeShops = (TextView) findViewById(R.id.tv_like_shops);
        tvMyShare = (TextView) findViewById(R.id.tv_my_share);
        tvBrowingHistory = (TextView) findViewById(R.id.tv_browing_history);

        ivSetting = (ImageView) findViewById(R.id.iv_setting);
        ivMessage = (ImageView) findViewById(R.id.iv_message);
        ivUserImg = (ImageView) findViewById(R.id.iv_user_img);

        llLeftMoney = (LinearLayout) findViewById(R.id.ll_left_money);
        llRedEnvelop = (LinearLayout) findViewById(R.id.ll_red_envelop);
        llIntegral = (LinearLayout) findViewById(R.id.ll_integral);
        llCoupon = (LinearLayout) findViewById(R.id.ll_coupon);

        llContent = (LinearLayout) findViewById(R.id.ll_content);
        llNologin = (LinearLayout) findViewById(R.id.ll_nologin);
        tvLogin = (TextView) findViewById(R.id.tv_login);
    }

    @Override
    public void onClick(View view) {
        if(!Const.isLogin){
            startActivity(new Intent(mActivity,LoginActivity.class));
            return;
        }
        switch (view.getId()){
            case R.id.tv_myorder:
            case R.id.tv_all_myorder:
                startActivity(new Intent(mActivity, OrderDetailActivity.class).putExtra("index",0));
                break;
            case R.id.tv_unpay:
                startActivity(new Intent(mActivity, OrderDetailActivity.class).putExtra("index",1));
                break;
            case R.id.tv_unreceipt:
                startActivity(new Intent(mActivity, OrderDetailActivity.class).putExtra("index",2));
                break;
            case R.id.tv_unevluated:
                startActivity(new Intent(mActivity, EvaluateActivity.class));
                break;
            case R.id.tv_return_or_change:
                startActivity(new Intent(mActivity,ReturnOrChangeActivity.class));
                break;
            case R.id.tv_mywallet:
            case R.id.tv_mange_money:
            case R.id.ll_left_money:
                startActivity(new Intent(mActivity, MangeMoneyActivity.class));
                break;
            case R.id.ll_integral:
                break;
            case R.id.ll_coupon:
                startActivity(new Intent(mActivity, RedEnvelopActivity.class).putExtra(TYPE,TYPE_COUPON));
                break;
            case R.id.ll_red_envelop:
                startActivity(new Intent(mActivity, RedEnvelopActivity.class).putExtra(TYPE,TYPE_RED));
                break;
            case R.id.tv_collect_products:
                startActivity(new Intent(mActivity, CollectProductActivity.class));
                break;
            case R.id.tv_like_shops:
                startActivity(new Intent(mActivity, LikeShopActivity.class));
                break;
            case R.id.tv_my_share:
                Intent intent = new Intent(mActivity, WebActivity.class);
                intent.putExtra(WebActivity.URL,Const.SHARE_URL);
                intent.putExtra(WebActivity.TITLE,"我的分享");
                startActivity(intent);
                break;
            case R.id.tv_browing_history:
                startActivity(new Intent(mActivity, BrowingHistoryActivity.class));
                break;
            case R.id.iv_setting:
                startActivity(new Intent(mActivity, SettingActivity.class));
                break;
            case R.id.iv_message:
                //开始登录
                if (App.mIMKit != null) {
                    Intent intent2 = App.mIMKit.getConversationActivityIntent();
                    startActivity(intent2);
                }else{
                    ToastUtil.showToast("YMKit未初始化！");
                }
                break;
        }
    }

    @Subscribe
    public void onEventMainThread(EventTest event) {
        if(event.getCode() == EventTest.LOGIN) requestData();
        else if(event.getCode() == EventTest.LOGINOUT) resetUI();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
